package com.hadoopSpark.application.test;

import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.security.KeyFactory;
import java.security.KeyStore;
import java.security.PrivateKey;
import java.security.cert.Certificate;
import java.security.cert.CertificateFactory;
import java.security.spec.PKCS8EncodedKeySpec;

import javax.net.ssl.SSLContext;

import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.ssl.SSLContexts;
import org.apache.http.util.EntityUtils;
import org.bouncycastle.openssl.PEMKeyPair;
import org.bouncycastle.openssl.PEMParser;

import com.aliyun.configuration.ConfigurationTool;
import com.certificate.tool.SetTrustStore;
import com.common.constant.Constant;

/* 2016.9.2
 * 在阿里云上，创建一个hadoop应用,
 * 测试，
 * 1、compose yaml 字符串  中传入参数
 * 2、想想如何根据用户传入的集群个数，集群名称，集群描述
 */
public class CreateHadoopSparkApp {
	
	public static void main(String[] args) {
		CreateHadoopSparkApp app=new CreateHadoopSparkApp();
		System.out.println(app.CreateHadoopCluster(args[0],args[1],args[2]));
	}
	public String CreateHadoopCluster(String nodes,String clusterName,String clusterDesc){
		String returnCode="400";
	 //get certificate address
	 try {
		 SSLConnectionSocketFactory sslsf = SetTrustStore.getSSLConnection();
		 //httpclient链接
		 CloseableHttpClient httpClient = HttpClients.custom().setSSLSocketFactory(sslsf).build();
		 
		 HttpPost httpPost = new HttpPost(ConfigurationTool.getProperties(Constant.ALIYUN_PROJECTS_URL));
		 
		 httpPost.addHeader("Content-type","application/json;charset=UTF-8");
		 
		 StringBuffer composeYaml = new StringBuffer();
		 //下面修改的是，master 容器的 编排模板
		 composeYaml.append(ConfigurationTool.getProperties(Constant.MASTER_Container_NAME_YAML))
			.append("  "+ConfigurationTool.getProperties(Constant.IMAGE_YAML))
			.append("  "+ConfigurationTool.getProperties(Constant.HOSTNAME_YAML)+ConfigurationTool.getProperties(Constant.MASTER_HOSTNAME_YAML)+ConfigurationTool.getProperties(Constant.NEWLIN_SYSBOL))
			.append("  "+ConfigurationTool.getProperties(Constant.PORTS_YAML))
			.append("  "+ConfigurationTool.getProperties(Constant.ENVIRONMENT_YAML))
			.append("  "+ConfigurationTool.getProperties(Constant.RESTART_YAML))
			.append("  "+ConfigurationTool.getProperties(Constant.VOLUMES_YAML)+ clusterName + "/" + ConfigurationTool.getProperties(Constant.MASTER_VOLUME_YAML))
			.append(ConfigurationTool.getProperties(Constant.VOLUMES_TO_CONYAML))
			.append("  "+ConfigurationTool.getProperties(Constant.OTHER_ARGS_YAML))
			.append(ConfigurationTool.getProperties(Constant.NEWLIN_SYSBOL))
			;
		 //集群中，节点的个数； 需要用户传入
		 int nodeNum = Integer.valueOf(nodes); 
//		 int nodeNum = Integer.valueOf(3); 
		 
		 for(int i = 1; i < nodeNum; i++ ){
			 
			 composeYaml.append(ConfigurationTool.getProperties(Constant.SLAVE_CONTAINER_NAME_YAML)+i+":"+ConfigurationTool.getProperties(Constant.NEWLIN_SYSBOL))
			 			.append("  "+ConfigurationTool.getProperties(Constant.IMAGE_YAML))
			 			.append("  "+ConfigurationTool.getProperties(Constant.HOSTNAME_YAML)+ConfigurationTool.getProperties(Constant.SLAVE_HOSTNAME_YAML)+i+ConfigurationTool.getProperties(Constant.SLAVE_HOSTNAME_SUFFIX_YAML)+ConfigurationTool.getProperties(Constant.NEWLIN_SYSBOL))
			 			.append("  "+ConfigurationTool.getProperties(Constant.PORTS_YAML))
			 			.append("  "+ConfigurationTool.getProperties(Constant.ENVIRONMENT_YAML))
			 			.append("  "+ConfigurationTool.getProperties(Constant.RESTART_YAML))
			 			.append("  "+ConfigurationTool.getProperties(Constant.VOLUMES_YAML)+ clusterName + "/" + ConfigurationTool.getProperties(Constant.SLAVE_HOSTNAME_YAML)+i)
			 			.append(ConfigurationTool.getProperties(Constant.VOLUMES_TO_CONYAML))
			 			.append("  "+ConfigurationTool.getProperties(Constant.OTHER_ARGS_YAML))
			 			.append(ConfigurationTool.getProperties(Constant.NEWLIN_SYSBOL))
			 			;
		 }
		 
		 /*
		  * appName: 集群的名称，
		  * appDes:  集群的描述
		  */
		 
		 String appName = clusterName;
//		 String appName = "jishutong";
		 
//		 String appDesc = "bigdata";
		 String appDesc = clusterDesc;

		 String  requestEntity =
				 "{\"name\":"+"\""+appName+"\","
				 
				+ "\"description\":"+"\""+appDesc+"\","
				
				+ "\"template\":"+"\""+composeYaml.toString()+"\""+","
				
				+ "\"version\":\"1.0\","
				
				+ "\"environment\":{\"isForSpark\":\"no\",\"nodesNum\":\"9\"}}";
		 
		 StringEntity stringEntity = new StringEntity(requestEntity, Charset.forName("UTF-8"));
		 
		 httpPost.setEntity(stringEntity);

		 //使用客户端向服务器发送数据
		 HttpResponse httpResponse = httpClient.execute(httpPost);
		 
		 if (httpResponse.getStatusLine().getStatusCode() == 201) {
			 
//			System.out.println(httpResponse.getProtocolVersion()
//					+ " " + httpResponse.getStatusLine().getStatusCode()
//					+ " " + httpResponse.getStatusLine().getReasonPhrase());
			
			returnCode="201";
			
		
		} else {
			
			HttpEntity resultEntity = httpResponse.getEntity();
			
			//返回结果是，服务器上，返回来的是json字符串数据
			String strResultEntity = EntityUtils.toString(resultEntity);
			
			System.out.println(strResultEntity);
			
			//System.out.println(httpResponse.getStatusLine().getStatusCode());
			returnCode=String.valueOf(httpResponse.getStatusLine().getStatusCode());
		}
	
		} catch (Exception e) {
			returnCode="400";
			e.printStackTrace();
		}
	 return returnCode;
		
	}
}
		
		
		
